package main

import (
	"fmt"
	"time"
)

func main() {
	run()

	run2()
}

func run() {
	defer d1Trace1(time.Now())
	time.Sleep(500 * time.Millisecond)
}

func d1Trace1(begin time.Time) {
	elapsed := time.Since(begin)
	fmt.Println("elapsed:", elapsed)
}

/*
优化后，不用参数，返回函数。
*/
func run2() {
	defer d1Trace2()()
	time.Sleep(500 * time.Millisecond)
}

func d1Trace2() func() {
	begin := time.Now()
	return func() {
		elapsed := time.Since(begin)
		fmt.Println("elapsed:", elapsed)
	}
}
